package com.linghui.videoplus.ipai.utils;

import java.io.File;

import com.linghui.videoplus.ipai.VideoPlusApplication;

import android.content.Context;

public class ImageCacheUtils {
	private Context mContext;
	private static int mCacheSize = 150; // 设置的缓存大小。
	private final static long MB = 1073741824;  

	public ImageCacheUtils(Context context) {
		mContext = context;
	}

	/**
	 * 检查文件目录是否超过规定的缓存大小
	 * 
	 * @param fileName
	 */
	private void updateSizeCache(String pathFileName) {
		if (VideoPlusApplication.isSDCardEnable()) {
			File dirFile = new File(VideoPlusApplication.imageCacheRoot);
			File[] files = dirFile.listFiles();
			long dirSize = 0;
			for (File file : files) {
				dirSize += file.length();
			}
			if (dirSize > mCacheSize * MB) {
				clearLocalCache();
			}
		}
	}

	/**
	 * 清除本地缓存
	 */
	private void clearLocalCache() {
		if (VideoPlusApplication.isSDCardEnable()) {
			File dir = new File(VideoPlusApplication.imageCacheRoot);
			File[] files = dir.listFiles(); // 取得该目录下的所有文件
			if (files == null || files.length == 0) {
				return;
			}
			for (File file : files) {
				file.delete();
			}
		}
	}
}
